package com.zsix.system.strategy.autoCopyFor;

import com.zsix.system.service.ISmsEntityService;
import com.zsix.system.strategy.copyFor.CCContext;
import com.zsix.system.strategy.copyFor.CCStrategy;
import com.zsix.common.dto.controllerParams.CCParams;
import com.zsix.common.dto.controllerParams.ProcessTaskParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class ByUsersStrategy implements AutoCopyStrategy {
    @Autowired
    private CCContext ccContext;
    @Autowired
    private ISmsEntityService smsEntityService;

    /**
     * 执行知会
     * @param proc_inst_id
     * @param task_def_key
     * @return
     */
    @Override
    public String sendCopyFor(String proc_inst_id,String task_def_key){
        //设置查询参数
        CCParams ccParams=new CCParams();
        ccParams.setAutoCCWay("byUsers");
        ccParams.setTask_key(task_def_key);
        //获取服务
        CCStrategy ccStrategyServive=ccContext.getService("byUsers");
        //获取设置的自动抄送数据
        List<HashMap<String,Object>> hashMapList=ccStrategyServive.getSelectData(ccParams);
        //组织知会人员参数
        List<ProcessTaskParams.CCData> ccDataList= Optional.ofNullable(hashMapList).map(list->list.stream().map(m->{
            ProcessTaskParams.CCData ccData=new ProcessTaskParams.CCData();
            ccData.setId(m.get("id").toString());
            ccData.setName(m.get("name").toString());
            ccData.setUsername(m.get("username").toString());
            return ccData;
        }).collect(Collectors.toList())).orElse(new ArrayList<>());
        //组织参数
        ProcessTaskParams processTaskParams=new ProcessTaskParams();
        processTaskParams.setProcessInstanceId(proc_inst_id);
        processTaskParams.setCcData(ccDataList);
        processTaskParams.setTaskId(task_def_key);
        //执行知会
        smsEntityService.sendCopyForMsg(processTaskParams);
        return "执行成功";
    }
    @Override
    public String getType(){
        return "byUsers";
    }
}
